<?php
require_once dirname(__FILE__) . '/../includes/common.inc.php';
require_once dirname(__FILE__) . '/../includes/pdoorm.inc.php';

header('Content-Type: text/plain');


//$action = MiscUtils::getParam('name', NULL);

$class = MiscUtils::getParam('name', NULL);
$objectsToAddOrUpdate = json_decode(MiscUtils::getParam('obj', NULL));
$objectsToRemove = json_decode(MiscUtils::getParam('r', NULL));



    foreach ($objectsToAddOrUpdate as &$object) {
      if (isset($object->id) || isset($object->e_oid)) {
      	
	      switch($class){
	      	case'v1sample':
	      		pdouv1samplepdate($object,$class);
	      	    break;
	      		
	      	default:
	      		pdoupdate($object,$class);
	      		break;	
		
	      }
        
      } else {
	      	  switch($class){
		      	case'v1sample':
		      		pdouv1sampleadd($object,$class);
		      	    break;
		      		
		      	default:
		      		 pdoadd($object,$class);
		      		break;	
			
		      }      	
       
      }
    }
    
    foreach ($objectsToRemove as &$object) {
      
    	  switch($class){
	      	case'v1sample':
	      		pdouv1samplemove($object,$class);
	      	    break;
	      		
	      	default:
	      		pdoremove($object,$class);
	      		break;	
		
	      }      
    }


function pdoadd($obj,$class){
	
	$addsql="insert into ns_".$class."  values(NULL,";
	
	foreach ($obj as $key => $value){
		
			switch($class){
				case'datezone':
					{
						if( in_array($key,array('start','end') ) ) $value = SimpleDate::toStamp($value); //把日期转化为时间戳
					}
					break;
				
			}
				
	   $addsql.=" '".addslashes($value)."',";
	}

	$addsql =substr($addsql,0,-1);
	$addsql.=")";

	sql_execute($addsql);
}


function pdouv1sampleadd($obj,$class){
   
	$addsql ="insert into ns_".$class." (e_oid,trackId,name,price,unit,type,model) values(NULL,'".$obj->trackId."','".addslashes($obj->name)."','".$obj->price."','".$obj->unit."','".$obj->type."','".$obj->model."')";
	sql_execute($addsql);
	$parentId0 = lastinsertid();
	
	foreach($obj->attributes as $ars){
		$addsql0 ="insert into ns_".$class."attribute (e_oid,name,parentId0) values(NULL,'".addslashes($ars->name)."','".$parentId0."')";
	    sql_execute($addsql0);
	    $parentId1 = lastinsertid();	

	    foreach($ars->options as $ops){
	    	$addsql1 ="insert into ns_".$class."attributeoption (e_oid,name,parentId0,parentId1) values(NULL,'".addslashes($ops->name)."','".$parentId0."','".$parentId1."')";
	    	sql_execute($addsql1);
	    	
	    }
		
	}
}
    
    
function pdoupdate($obj,$class){
	
	$id=$obj->id;
	
	$upsql="update ns_".$class." set ";
	
	foreach ($obj as $key => $value){
		
		if(!in_array($key ,array('id','e_oid','trackId') )){
			$upsql.= $key."='".addslashes($value)."',";
		}
		
	}
	
	
	$upsql =substr($upsql,0,-1);
	$upsql.=" where e_oid='".$id."'";
	
	
	
    sql_execute($upsql);
	
}


function pdoremove($obj,$class){
	
	$id = $obj->id;
	$delsql="delete from ns_".$class." where e_oid='".$id."'";
	
	sql_execute($delsql);
	
}




?>